<?php
/**
 *$Id$
*/
class Gold_Trackingnumber_Model_Orderpreparation_CarrierTemplate extends Gold_Orderpreparation_Model_CarrierTemplate
{	
	/**
	 * Import tracking file
	 *
	 */
	public function importTrackingFile($t_lines)
	{
		$importedTrackingCount = 0;
		$skippedTrackingCount = 0;
		$debug = '';
		
		for($i=0;$i<count($t_lines);$i++)
		{
			//skip first line (if required)
			$line = $t_lines[$i];
			if (($i == 0) && ($this->getct_import_skip_first_record()))		
				continue;	

			$tracking = null;
			$shipmentReference = null;
				
			//parse fixed format line
			if ($this->getct_import_format() == 'fixed')
			{
				$currentPosition = 0;

				foreach ($this->getFields('import') as $field)
				{
					$size = $field->getctf_size();
					$fieldValue = substr($line, $currentPosition, $size);
					
					switch ($field->getctf_content()) {
						case 'tracking':
							$tracking = trim($fieldValue);
							break;
						case 'shipment':
							$shipmentReference = trim($fieldValue);
							break;
					}
					
					$currentPosition += $size;
				}
			}
			
			//parse delimiter format line
			if ($this->getct_import_format() == 'delimiter')
			{
				//split fields
				$t_columns = explode($this->getFieldSeparator('import'), $line);		
				foreach ($this->getFields('import') as $field)
				{
					if (isset($t_columns[$field->getctf_position()]))
					{
						$fieldValue = $t_columns[$field->getctf_position()];
						if ($this->getFieldDelimiter('import') != '')
							$fieldValue = str_replace($this->getFieldDelimiter('import'), '', $fieldValue);
						
						switch ($field->getctf_content()) {
							case 'tracking':
								$tracking = $fieldValue;
								break;
							case 'shipment':
								$shipmentReference = $fieldValue;
								break;
						}
					}
				}
			}		
			//add tracking
			if (($tracking != null) && ($shipmentReference != null))
			{
				$result = mage::helper('Orderpreparation/Tracking')->addTrackingToShipment($tracking, $shipmentReference, $this->getct_shipping_method());
				if ($result)
				{ 
				    $shipment = mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentReference);
					Mage::helper('Orderpreparation')->notifyShipment($shipment->getId());
					$importedTrackingCount++;	
				}
				else 
					$skippedTrackingCount++;
			}
			else 
				$debug .= 'Unable to retrieve shipment or/and tracking for line #'.$i."\n";
			
		}
		
		mage::log($debug);
		$msg = mage::helper('Orderpreparation')->__('Tracking import complete : %s tracking imported, %s tracking skipped', $importedTrackingCount, $skippedTrackingCount);
		return $msg;
	}	
	
	/**
	 * return char separator between fields for export file
	 *
	 * @return unknown
	 */
	private function getFieldSeparator($type)
	{
		$code = '';
		if ($type == 'import')
			$code = $this->getct_import_file_separator();
		else 
			$code = $this->getct_export_file_separator();
		
		switch ($code)
		{
			case 'coma':
				return ',';
				break;
			case 'semicolon':
				return ';';
				break;
			case 'tab':
				return chr(9);
				break;
		}
	}
	
	
	/**
	 * return char separator between fields for export file
	 *
	 * @return unknown
	 */
	private function getFieldDelimiter($type)
	{
		$code = '';
		if ($type == 'import')
			$code = $this->getct_import_file_delimiter();
		else 
			$code = $this->getct_export_file_delimiter();
		
		switch ($code)
		{
			case 'quote':
				return "'";
				break;
			case 'doublequote':
				return '"';
				break;
		}
	}
	
	/**
	 * Return char to end line
	 *
	 * @return unknown
	 */
	private function getLineDelimiter()
	{
		switch ($this->getct_export_line_end())
		{
			case 'n':
				return chr(13);
				break;
			case 'r':
				return chr(10);		
				break;
			case 'rn':
				return chr(13).chr(10);				
				break;
		}
	}

}